// 小程序打包源码 拷贝自微信开发者工具
 const a = require('glob')
          , b = require('fs')
          , c = require('path')
          // , d = require('./72653d4b93cdd7443296229431a7aa9a.js')
          , e = require('crypto');

// f C:\Users\Administrator\AppData\Local\微信web开发者工具\User Data\Weappdest\1504687864016
// g C:\Users\Administrator\AppData\Local\微信web开发者工具\User Data\Weappdest\1504687864016.wx
module.exports = async(f,g,h={})=>{
            var i = [new Buffer(1), new Buffer(4), new Buffer(4), new Buffer(4), new Buffer(1)];
            i[0].writeIntLE(190),
            i[1].writeInt32BE(1),
            i[4].writeIntLE(237);
            let j = 0
              , k = []
              , l = []
              , m = []
              , n = []
              , o = [];
            return new Promise((p,q)=>{
                let r = Object.assign({
                    nodir: !0
                }, h)
                  , s = {};
                a(`${f}/**`, r, (a,r)=>{
                    if (!a) {
                        r.forEach((a)=>{
                            let d = b.readFileSync(a)
                              , g = c.relative(f, a);
                            if (h.needMd5) {
                                let a = e.createHash('md5');
                                a.update(d);
                                let b = a.digest('hex');
                                if (s[g] = b,
                                h.ignoreFileMd5 && h.ignoreFileMd5[g] == b)
                                    return
                            }
                            let i = new Buffer(`/${g.replace(/\\/g, '/')}`);
                            j++,
                            m.push(i),
                            n.push(d)
                        }
                        );
                        let a = 18 + 12 * j + Buffer.concat(m).length;
                        l = m.map((b,c)=>{
                            let d = new Buffer(4);
                            d.writeInt32BE(b.length);
                            let e = new Buffer(4)
                              , f = n[c].length
                              , g = a;
                            e.writeInt32BE(g),
                            a += f;
                            let h = new Buffer(4);
                            return h.writeInt32BE(f),
                            Buffer.concat([d, b, e, h])
                        }
                        );
                        let q = new Buffer(4);
                        q.writeInt32BE(j),
                        l.unshift(q),
                        k = Buffer.concat(l),
                        o = Buffer.concat(n),
                        i[2].writeInt32BE(k.length),
                        i[3].writeInt32BE(o.length),
                        i = Buffer.concat(i);
                        let t = Buffer.concat([i, k, o]);
                        b.writeFileSync(g, t),
                        // d.info(`pack.js create ${g} success!`),
                        p({
                            destPath: g,
                            data: t,
                            fileMd5Info: s
                        })
                    } else
                        q(a)
                }
                )
            }
            )
        }

